package cn.androidstudy.listviewdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private String[] names = new String[]{"京东", "天猫", "UC"};
    private String[] says = new String[]{"刘强东的商城", "马云的商城", "一款手机浏览器"};
    private int[] imgIds = new int[]{R.drawable.jd, R.drawable.tm, R.drawable.uc};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btnClick(View v){
        //要显示的数据，M，获取数据，保存
        String[] strs = {"郑州","开封","洛阳","新乡","安阳"};
        //创建ArrayAdapter：C，控制器
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this,
                        android.R.layout.simple_list_item_1,//为Item指定布局
                        strs);//指定要显示的数据
        //获取ListView对象，通过调用setAdapter方法为ListView设置Adapter设置适配器
        ListView list_test = findViewById(R.id.listview);//V:视图，显示数据
        list_test.setAdapter(adapter);
    }

    public void btnSimpleClick(View v){

        List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < names.length; i++) {
            Map<String, Object> showitem = new HashMap<String, Object>();
            showitem.put("touxiang", imgIds[i]);
            showitem.put("name", names[i]);
            showitem.put("says", says[i]);
            listitem.add(showitem);
        }

        //创建一个simpleAdapter
        SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(),
                listitem,//要显示的数据
                R.layout.list_item, //指定Item的布局  View对象
                new String[]{"touxiang", "name"},//listitem中数据的key
                new int[]{R.id.imgtou, R.id.name});//Item布局的组件id
        ListView listView = findViewById(R.id.listview);
        listView.setAdapter(myAdapter);
    }

    public void btnBaseClick(View v){
        List<AppBean> list=new ArrayList<>();
        AppBean bean;
        for(int i=0;i<names.length;i++){
            bean=new AppBean(names[i],says[i],imgIds[i]);
            list.add(bean);
        }

        ListView listView = findViewById(R.id.listview);
        //最初的适配器
        //listView.setAdapter(new MyAdapter());
        //使用改进后的适配器
        //listView.setAdapter(new MyAdapterNew(this,list));
        //使用通用适配器
        listView.setAdapter(new CommonAdapter<AppBean>(this,list,R.layout.list_item) {
            @Override
            public void convert(ViewHolder helper, AppBean item) {
                helper.setText(R.id.name,item.getName());
                helper.setText(R.id.says,item.getDesc());
                helper.setImageResource(R.id.imgtou,item.getImgId());
            }
        });
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {//决定ListView中Item的数量
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        //为每个Item生成视图
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //1.加载布局
            View view= LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.list_item,parent,false);
            //2.设置view中组件的属性
            ImageView imageView=view.findViewById(R.id.imgtou);
            TextView tvName=view.findViewById(R.id.name);
            TextView tvSays=view.findViewById(R.id.says);
            imageView.setImageResource(imgIds[position]);
            tvName.setText(names[position]);
            tvSays.setText(says[position]);
            //3.返回view，将绘制到Item
            return view;
        }
    }
}
